package com.zcf.shiro.web.service.impl;

import com.zcf.shiro.web.bean.Permissions;
import com.zcf.shiro.web.bean.Role;
import com.zcf.shiro.web.bean.User;
import com.zcf.shiro.web.service.LoginService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author zhaochaofeng
 * @date 2020/6/17 11:11
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Override
    public User getUserById(String userId) {

        //管理员，拥有访问/manage/test/query的权限
        Permissions permissions1 = new Permissions();
        permissions1.setId("123");
        permissions1.setUrl("/manage/test/query");
        permissions1.setPermissionsName("管理测试查询");
        Set<Permissions> permissionsSet1 = new HashSet<>();
        permissionsSet1.add(permissions1);
        Role role1 = new Role();
        role1.setId("admin");
        role1.setRoleName("管理员");
        role1.setPermissions(permissionsSet1);
        Set<Role> roleSet1 = new HashSet<>();
        roleSet1.add(role1);
        User user1 = new User();
        user1.setId("admin");
        user1.setPassword(new SimpleHash("MD5", "123456", ByteSource.Util.bytes("admin"), 5).toString());
        user1.setUserName("管理员");
        user1.setRoles(roleSet1);

        //客户，拥有访问/member/test/query的权限
        Permissions permissions2 = new Permissions();
        permissions2.setId("234");
        permissions2.setUrl("/member/test/query");
        permissions2.setPermissionsName("客户测试查询");
        Set<Permissions> permissionsSet2 = new HashSet<>();
        permissionsSet2.add(permissions2);
        Role role2 = new Role();
        role2.setId("customer");
        role2.setRoleName("客户");
        role2.setPermissions(permissionsSet2);
        Set<Role> roleSet2 = new HashSet<>();
        roleSet2.add(role2);
        User user2 = new User();
        user2.setId("test");
        user2.setPassword(new SimpleHash("MD5", "123456", ByteSource.Util.bytes("test"), 5).toString());
        user2.setUserName("客户");
        user2.setRoles(roleSet2);

        Map<String ,User> map = new HashMap<>();
        map.put(user1.getId(), user1);
        map.put(user2.getId(), user2);
        return map.get(userId);
    }
}
